/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sbmart.smartserver.service;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sbmart.object.*;
import sbmart.smartserver.cache.ConnectionManager;

/**
 *
 * @author albert
 */
public class ServerTrsService {

    private static ServerTrsService instance;
    public static final Logger LOG = LoggerFactory.getLogger("ClientTrs");

    private ServerTrsService() {
    }

    public static ServerTrsService instance() {
        if (instance == null) {
            instance = new ServerTrsService();
        }
        return instance;
    }

    public synchronized Connection putTrsBkl(Header header, List<TrsBklMst> data, Connection serverConn) throws SQLException {
        LOG.debug("[PUT]");
        CallableStatement call_put_trs_bkl_mst = serverConn.prepareCall(ServerProcedures.put_trs_bkl_mst);
        CallableStatement call_put_trs_bkl_detail = serverConn.prepareCall(ServerProcedures.put_trs_bkl_detail);
        for (TrsBklMst mst : data) {
            call_put_trs_bkl_mst.setInt("bkl_id", mst.getBklId());
            call_put_trs_bkl_mst.setString("trans_num", mst.getTransNum());
            call_put_trs_bkl_mst.setShort("branch_id", mst.getBranchId());

            if (mst.getTransDate() != null) {
                call_put_trs_bkl_mst.setDate("trans_date", new java.sql.Date(mst.getTransDate().getTimeInMillis()));
            } else {
                call_put_trs_bkl_mst.setDate("trans_date", null);
            }
            call_put_trs_bkl_mst.setByte("jenis_pajak", mst.getJenisPajak());
            call_put_trs_bkl_mst.setShort("relation_id", mst.getRelationId());
            call_put_trs_bkl_mst.setString("no_faktur", mst.getNoFaktur());
            call_put_trs_bkl_mst.setFloat("subtotal", mst.getSubtotal());
            call_put_trs_bkl_mst.setFloat("discount", mst.getDiscount());
            call_put_trs_bkl_mst.setFloat("tax", mst.getTax());
            call_put_trs_bkl_mst.setFloat("total", mst.getTotal());
            call_put_trs_bkl_mst.setString("divisi", mst.getDivisi());
            call_put_trs_bkl_mst.setByte("disc_go", mst.getDiscGo());
            call_put_trs_bkl_mst.setString("notes", mst.getNotes());
            call_put_trs_bkl_mst.setByte("top_buying", mst.getTopBuying());
            call_put_trs_bkl_mst.setDate("faktur_date", new java.sql.Date(mst.getFakturDate().getTimeInMillis()));

            call_put_trs_bkl_mst.registerOutParameter(1, Types.INTEGER);
            call_put_trs_bkl_mst.execute();
            int identity = call_put_trs_bkl_mst.getInt(1);

            if (identity != 0) {
                List<TrsBklDetail> details = mst.getDetails();
                for (TrsBklDetail detail : details) {
                    call_put_trs_bkl_detail.setInt("bkl_id", identity);
                    call_put_trs_bkl_detail.setInt("item_id", detail.getItemId());
                    call_put_trs_bkl_detail.setInt("branch_id", detail.getBranchId());
                    call_put_trs_bkl_detail.setFloat("quantity2", detail.getQuantity2());
                    call_put_trs_bkl_detail.setFloat("qty_bonus", detail.getQtyBonus());
                    call_put_trs_bkl_detail.setFloat("price", detail.getPrice());
                    call_put_trs_bkl_detail.setFloat("disc", detail.getDisc());
                    call_put_trs_bkl_detail.setFloat("tax", detail.getTax());
                    call_put_trs_bkl_detail.setFloat("amount", detail.getAmount());
                    call_put_trs_bkl_detail.setFloat("frac_qty", detail.getFracQty());
                    call_put_trs_bkl_detail.setFloat("frac_price", detail.getPrice());
                    call_put_trs_bkl_detail.setFloat("frac_bonus", detail.getFracBonus());
                    call_put_trs_bkl_detail.setInt("item_bonus_id", detail.getItemBonusId());
                    call_put_trs_bkl_detail.setFloat("disc_value_pcs", detail.getDiscValuePcs());
                    call_put_trs_bkl_detail.setInt("hpp_id", detail.getHppId());
                    call_put_trs_bkl_detail.setByte("jenis_bpb", detail.getJenisBpb());
                    call_put_trs_bkl_detail.setFloat("disc_exclude_oi", detail.getDiscExcludeOi());
                    call_put_trs_bkl_detail.setFloat("disc_go", detail.getDiscGo());
                    call_put_trs_bkl_detail.setString("notes", detail.getNotes());
                    call_put_trs_bkl_detail.execute();
                }
            } else {
                LOG.debug("[ERROR]:insert detail failed");
            }
        }
        return serverConn;
    }

    public synchronized Connection putTrsOrderIntern(Header header, List<TrsPbGeraiMst> data, Connection serverConn) throws SQLException {
        LOG.debug("[PUT]");
        CallableStatement call_put_trs_order_intern_mst = serverConn.prepareCall(ServerProcedures.put_trs_order_intern_mst);
        CallableStatement call_put_trs_order_intern_detail = serverConn.prepareCall(ServerProcedures.put_trs_order_intern_detail);

        for (TrsPbGeraiMst mst : data) {

            call_put_trs_order_intern_mst.setInt("pb_id", mst.getPbId());
            call_put_trs_order_intern_mst.setString("trans_num", mst.getTransNum());
            call_put_trs_order_intern_mst.setDate("trans_date", new java.sql.Date(mst.getTransDate().getTimeInMillis()));
            call_put_trs_order_intern_mst.setShort("branch_id", mst.getBranchId());
            call_put_trs_order_intern_mst.setString("users_id", mst.getUsersId());
            call_put_trs_order_intern_mst.setString("notes", mst.getNotes());
            call_put_trs_order_intern_mst.setByte("pb_type", mst.getPbType());
            call_put_trs_order_intern_mst.setFloat("subtotal", mst.getSubtotal());
            call_put_trs_order_intern_mst.setFloat("tax", mst.getTax());
            call_put_trs_order_intern_mst.setFloat("total", mst.getTotal());
            call_put_trs_order_intern_mst.setString("insert_log", mst.getInsertLog());

            call_put_trs_order_intern_mst.registerOutParameter(1, Types.INTEGER);
            call_put_trs_order_intern_mst.execute();
            int identity = call_put_trs_order_intern_mst.getInt(1);

            if (identity != 0) {
                List<TrsPbGeraiDetail> details = mst.getDetails();
                for (TrsPbGeraiDetail detail : details) {
                    call_put_trs_order_intern_detail.setInt("poi_id", identity);
                    call_put_trs_order_intern_detail.setInt("item_id", detail.getItemId());
                    call_put_trs_order_intern_detail.setFloat("quantity", detail.getQuantity());
                    call_put_trs_order_intern_detail.setFloat("price", detail.getPrice());
                    call_put_trs_order_intern_detail.setFloat("tax", detail.getTax());
                    call_put_trs_order_intern_detail.setFloat("amount", detail.getAmount());
                    call_put_trs_order_intern_detail.setByte("is_tax", detail.getIsTax());

                    call_put_trs_order_intern_detail.execute();
                }
            } else {
                LOG.debug("[ERROR]:insert detail failed");
            }
        }
        return serverConn;
    }

    public synchronized Connection putTrsReturBkl(Header header, List<TrsReturBklMst> data, Connection serverConn) throws SQLException {
        LOG.debug("[PUT]");
        CallableStatement call_put_trs_retur_bkl_mst = serverConn.prepareCall(ServerProcedures.put_trs_retur_bkl_mst);
        CallableStatement call_put_trs_retur_bkl_detail = serverConn.prepareCall(ServerProcedures.put_trs_retur_bkl_detail);
        for (TrsReturBklMst mst : data) {
            call_put_trs_retur_bkl_mst.setInt("retur_bkl_id", mst.getReturBklId());
            call_put_trs_retur_bkl_mst.setString("trans_num", mst.getTransNum());
            call_put_trs_retur_bkl_mst.setShort("branch_id", mst.getBranchId());
            call_put_trs_retur_bkl_mst.setString("trans_num", mst.getTransNum());
            call_put_trs_retur_bkl_mst.setDate("trans_date", new java.sql.Date(mst.getTransDate().getTimeInMillis()));
            call_put_trs_retur_bkl_mst.setShort("relation_id", mst.getRelationId());
            call_put_trs_retur_bkl_mst.setFloat("subtotal", mst.getSubtotal());
            call_put_trs_retur_bkl_mst.setFloat("discount", mst.getDiscount());
            call_put_trs_retur_bkl_mst.setFloat("total", mst.getTotal());
            call_put_trs_retur_bkl_mst.setString("notes", mst.getNotes());
            call_put_trs_retur_bkl_mst.setFloat("tax", mst.getTax());

            call_put_trs_retur_bkl_mst.registerOutParameter(1, Types.INTEGER);

            call_put_trs_retur_bkl_mst.execute();
            int identity = call_put_trs_retur_bkl_mst.getInt(1);

            if (identity != 0) {
                List<TrsReturBklDetail> details = mst.getDetails();
                for (TrsReturBklDetail detail : details) {
                    call_put_trs_retur_bkl_detail.setInt("retur_id", identity);
                    call_put_trs_retur_bkl_detail.setInt("branch_id", header.getBranchId());
                    call_put_trs_retur_bkl_detail.setInt("item_id", detail.getItemId());
                    call_put_trs_retur_bkl_detail.setFloat("quantity", detail.getQuantity());
                    call_put_trs_retur_bkl_detail.setFloat("price", detail.getPrice());
                    call_put_trs_retur_bkl_detail.setFloat("discount", detail.getDiscount());
                    call_put_trs_retur_bkl_detail.setFloat("tax", detail.getTax());
                    call_put_trs_retur_bkl_detail.setFloat("amount", detail.getAmount());

                    call_put_trs_retur_bkl_detail.execute();
                }
            } else {
                LOG.debug("[ERROR]:insert detail failed");
            }
        }
        return serverConn;
    }

    public synchronized Connection putTrsReturGerai(Header header, List<TrsReturGeraiMst> data, Connection serverConn) throws SQLException {
        LOG.debug("[PUT]");
        CallableStatement call_put_trs_retur_gerai_mst = serverConn.prepareCall(ServerProcedures.put_trs_retur_gerai_mst);
        CallableStatement call_put_trs_retur_gerai_detail = serverConn.prepareCall(ServerProcedures.put_trs_retur_gerai_detail);

        for (TrsReturGeraiMst mst : data) {
            call_put_trs_retur_gerai_mst.setInt("retur_gr_id", mst.getReturGrId());
            call_put_trs_retur_gerai_mst.setInt("trans_num", mst.getBranchId());
            call_put_trs_retur_gerai_mst.setString("delivery_num", mst.getTransNum());

            if (mst.getTransDate() != null) {
                call_put_trs_retur_gerai_mst.setDate("delivery_date", new java.sql.Date(mst.getTransDate().getTimeInMillis()));
            } else {
                call_put_trs_retur_gerai_mst.setDate("delivery_date", null);
            }
            call_put_trs_retur_gerai_mst.setShort("branch_dest", mst.getBranchId());
            call_put_trs_retur_gerai_mst.setShort("branch_sorc", mst.getBranchId());
            call_put_trs_retur_gerai_mst.setString("notes", mst.getNotes());
            call_put_trs_retur_gerai_mst.setByte("retur_type", mst.getReturType());
            call_put_trs_retur_gerai_mst.setString("users_id", mst.getUsersId());
            call_put_trs_retur_gerai_mst.setFloat("tax", mst.getTax());
            call_put_trs_retur_gerai_mst.setFloat("subtotal", mst.getSubtotal());
            call_put_trs_retur_gerai_mst.setFloat("total", mst.getTotal());

            call_put_trs_retur_gerai_mst.registerOutParameter(1, Types.INTEGER);

            call_put_trs_retur_gerai_mst.execute();
            int identity = call_put_trs_retur_gerai_mst.getInt(1);

            if (identity != 0) {
                List<TrsReturGeraiDetail> details = mst.getDetails();
                for (TrsReturGeraiDetail detail : details) {
                    call_put_trs_retur_gerai_detail.setInt("terima_id", identity);
                    call_put_trs_retur_gerai_detail.setInt("item_id", detail.getItemId());
                    call_put_trs_retur_gerai_detail.setFloat("quantity", detail.getQuantity());
                    call_put_trs_retur_gerai_detail.setFloat("price", detail.getPrice());
                    call_put_trs_retur_gerai_detail.setInt("rusak_id", detail.getRusakId());
                    call_put_trs_retur_gerai_detail.setFloat("tax", detail.getTax());
                    call_put_trs_retur_gerai_detail.setFloat("amount", detail.getAmount());

                    call_put_trs_retur_gerai_detail.execute();
                }
            } else {
                LOG.debug("[ERROR]:insert detail failed");
            }
        }
        return serverConn;
    }

    public synchronized Connection putTrsReturOther(Header header, List<TrsReturOtherMst> data, Connection serverConn) throws SQLException {
        LOG.debug("[PUT]");
        CallableStatement call_put_trs_retur_other_mst = serverConn.prepareCall(ServerProcedures.put_trs_retur_other_mst);
        CallableStatement call_put_trs_retur_other_detail = serverConn.prepareCall(ServerProcedures.put_trs_retur_other_detail);

        for (TrsReturOtherMst mst : data) {

            call_put_trs_retur_other_mst.setInt("retur_other_id", mst.getReturOtherId());
            if (mst.getTransDate() != null) {
                call_put_trs_retur_other_mst.setDate("delivery_date", new java.sql.Date(mst.getTransDate().getTimeInMillis()));
            } else {
                call_put_trs_retur_other_mst.setDate("delivery_date", null);
            }
            call_put_trs_retur_other_mst.setShort("branch_id", mst.getBranchId());
            call_put_trs_retur_other_mst.setString("delivery_num", mst.getTransNum());
            call_put_trs_retur_other_mst.setShort("gudang_id", mst.getGudangId());
            call_put_trs_retur_other_mst.setString("notes", mst.getNotes());
            call_put_trs_retur_other_mst.setFloat("subtotal", mst.getSubtotal());
            call_put_trs_retur_other_mst.setFloat("total", mst.getTotal());

            call_put_trs_retur_other_mst.registerOutParameter(1, Types.INTEGER);

            call_put_trs_retur_other_mst.execute();
            int identity = call_put_trs_retur_other_mst.getInt(1);

            if (identity != 0) {
                List<TrsReturOtherDetail> details = mst.getDetails();
                for (TrsReturOtherDetail detail : details) {
                    call_put_trs_retur_other_detail.setInt("terima_other_id", identity);
                    call_put_trs_retur_other_detail.setInt("item_id", detail.getItemId());
                    call_put_trs_retur_other_detail.setFloat("quantity", detail.getQuantity());
                    call_put_trs_retur_other_detail.setFloat("price", detail.getPrice());
                    call_put_trs_retur_other_detail.setFloat("amount", detail.getAmount());

                    call_put_trs_retur_other_detail.execute();
                }
            } else {
                LOG.debug("[ERROR]:insert detail failed");
            }

        }
        return serverConn;
    }

    public synchronized Connection putMstItemStok(Header header, List<MstItemStock> data, Connection serverConn) throws SQLException {
        LOG.debug("[PUT]");
        CallableStatement call_put_mst_item_stok = serverConn.prepareCall(ServerProcedures.put_mst_item_stok);
        for (MstItemStock mst : data) {
            call_put_mst_item_stok.setInt("stok_id", mst.getStokId());
            call_put_mst_item_stok.setInt("item_id", mst.getItemId());
            call_put_mst_item_stok.setShort("branch_id", mst.getBranchId());
            call_put_mst_item_stok.setFloat("stok", mst.getStok());
            call_put_mst_item_stok.setString("insert_log", mst.getInsertLog());
            call_put_mst_item_stok.setString("edit_log", mst.getEditLog());

            call_put_mst_item_stok.execute();
        }
        return serverConn;

    }

    public synchronized Connection putTrsSalesVoucher(Header header, List<TrsSalesVoucher> list, Connection serverConn) throws SQLException {
        LOG.debug("[PUT]");
        CallableStatement call_put_trs_sales_voucher = serverConn.prepareCall(ServerProcedures.put_trs_sales_voucher);
        for (TrsSalesVoucher mst : list) {
            call_put_trs_sales_voucher.setInt("voucher_id", mst.getVoucherId());

            if (mst.getTransDate() != null) {
                call_put_trs_sales_voucher.setDate("trans_date", new java.sql.Date(mst.getTransDate().getTimeInMillis()));
            } else {
                call_put_trs_sales_voucher.setDate("trans_date", null);
            }

            call_put_trs_sales_voucher.setInt("branch_id", mst.getBranchId());
            call_put_trs_sales_voucher.setInt("branch_id", mst.getBranchId());
            call_put_trs_sales_voucher.setFloat("sales_id", mst.getSalesId());
            call_put_trs_sales_voucher.setFloat("amount", mst.getAmount());
            call_put_trs_sales_voucher.setString("voucher_code", mst.getVoucherCode());
            call_put_trs_sales_voucher.setString("insert_log", mst.getInsertLog());
            call_put_trs_sales_voucher.setString("edit_log", mst.getEditLog());
            call_put_trs_sales_voucher.setByte("jenis", mst.getJenis());

            call_put_trs_sales_voucher.execute();
        }
        return serverConn;
    }

    public synchronized Connection putUsedVoucherList(Header header, List<MstVoucher> list, Connection serverConn) throws SQLException {
        LOG.debug("[PUT]");
        CallableStatement call_put_used_voucher_list = serverConn.prepareCall(ServerProcedures.put_used_voucher_list);
        for (MstVoucher mst : list) {
            call_put_used_voucher_list.setInt("voucher_id", mst.getVoucherId());
            call_put_used_voucher_list.setInt("branch_id", mst.getBranchId());

            if (mst.getUsedDate() != null) {
                call_put_used_voucher_list.setDate("used_date", new java.sql.Date(mst.getUsedDate().getTimeInMillis()));
            } else {
                call_put_used_voucher_list.setDate("used_date", null);
            }

            call_put_used_voucher_list.execute();
        }
        return serverConn;
    }

    public synchronized Connection putTrsAdjustGerai(Header header, List<TrsAdjustGeraiMst> list, Connection serverConn) throws SQLException {
        LOG.debug("[PUT]");
        CallableStatement call_put_trs_adjust_gerai_mst = serverConn.prepareCall(ServerProcedures.put_trs_adjust_gerai_mst);
        CallableStatement call_put_trs_adjust_gerai_detail = serverConn.prepareCall(ServerProcedures.put_trs_adjust_gerai_detail);

        for (TrsAdjustGeraiMst trs : list) {
            call_put_trs_adjust_gerai_mst.setInt("adj_gerai_id", trs.getAdjGeraiId());
            call_put_trs_adjust_gerai_mst.setString("trans_num", trs.getTransNum());
            call_put_trs_adjust_gerai_mst.setInt("jenis", trs.getJenis());

            if (trs.getTransDate() != null) {
                call_put_trs_adjust_gerai_mst.setDate("trans_date", new java.sql.Date(trs.getTransDate().getTimeInMillis()));
            } else {
                call_put_trs_adjust_gerai_mst.setDate("trans_date", null);
            }

            call_put_trs_adjust_gerai_mst.setShort("branch_id", trs.getBranchId());
            call_put_trs_adjust_gerai_mst.setString("notes", trs.getNotes());
            call_put_trs_adjust_gerai_mst.setString("users_id", trs.getUsersId());
            call_put_trs_adjust_gerai_mst.setByte("status_id", trs.getStatusId());
            call_put_trs_adjust_gerai_mst.setByte("adj_type", trs.getAdjType());
            call_put_trs_adjust_gerai_mst.setString("insert_log", trs.getInsertLog());
            call_put_trs_adjust_gerai_mst.setString("edit_log", trs.getEditLog());

            call_put_trs_adjust_gerai_mst.registerOutParameter(1, Types.INTEGER);

            call_put_trs_adjust_gerai_mst.execute();
            int identity = call_put_trs_adjust_gerai_mst.getInt(1);

            if (identity != 0) {
                List<TrsAdjustGeraiDetail> details = trs.getDetails();
                for (TrsAdjustGeraiDetail detail : details) {
                    call_put_trs_adjust_gerai_detail.setInt("adj_id", identity);
                    call_put_trs_adjust_gerai_detail.setInt("item_id", detail.getItemId());
                    call_put_trs_adjust_gerai_detail.setInt("conv_id", detail.getConvId());
                    call_put_trs_adjust_gerai_detail.setFloat("quantity", detail.getQuantity());
                    call_put_trs_adjust_gerai_detail.setFloat("stok_system", detail.getStokSystem());
                    call_put_trs_adjust_gerai_detail.setFloat("conversion", detail.getConversion());
                    call_put_trs_adjust_gerai_detail.setShort("alasan_id", detail.getAlasanId());
                    call_put_trs_adjust_gerai_detail.setFloat("price", detail.getPrice());
                    call_put_trs_adjust_gerai_detail.setFloat("stok_value", detail.getStokValue());
                    call_put_trs_adjust_gerai_detail.setFloat("range_value", detail.getRangeValue());

                    call_put_trs_adjust_gerai_detail.execute();
                }
            } else {
                LOG.debug("[ERROR]:insert detail failed");
            }
        }
        return serverConn;
    }

    public synchronized Connection putTrsSalesOnline(Header header, List<TrsSalesOnline> list, Connection serverConn) throws SQLException {
        LOG.debug("[PUT]");
        CallableStatement call_put_trs_sales_online = serverConn.prepareCall(ServerProcedures.put_trs_sales_online);
        for (TrsSalesOnline mst : list) {
            call_put_trs_sales_online.setInt("sales_online_id", mst.getSalesOnlineId());
            call_put_trs_sales_online.setInt("branch_id", mst.getBranchId());

            if (mst.getTransDate() != null) {
                call_put_trs_sales_online.setTimestamp("trans_date", new java.sql.Timestamp(mst.getTransDate().getTimeInMillis()));
            } else {
                call_put_trs_sales_online.setTimestamp("trans_date", null);
            }

            call_put_trs_sales_online.setInt("rekap_id", mst.getRekapId());
            call_put_trs_sales_online.setInt("trans_type", mst.getTransType());
            call_put_trs_sales_online.setString("id_pelanggan", mst.getIdPelanggan());
            call_put_trs_sales_online.setString("nama", mst.getNama());
            call_put_trs_sales_online.setFloat("quantity", mst.getQuantity());
            call_put_trs_sales_online.setFloat("tagihan", mst.getTagihan());
            call_put_trs_sales_online.setFloat("adm", mst.getAdm());
            call_put_trs_sales_online.setFloat("materai", mst.getMaterai());
            call_put_trs_sales_online.setFloat("ppn", mst.getPpn());
            call_put_trs_sales_online.setFloat("ppj", mst.getPpj());
            call_put_trs_sales_online.setShort("media_bayar", mst.getMediaBayar());
            call_put_trs_sales_online.setFloat("cash", mst.getCash());
            call_put_trs_sales_online.setShort("card_id", mst.getCardId());
            call_put_trs_sales_online.setFloat("card", mst.getCard());
            call_put_trs_sales_online.setFloat("sumbangan", mst.getSumbangan());
            call_put_trs_sales_online.setFloat("voucher", mst.getVoucher());
            call_put_trs_sales_online.setFloat("total", mst.getTotal());
            call_put_trs_sales_online.setFloat("cash_back", mst.getCashBack());
            call_put_trs_sales_online.setString("respon", mst.getRespon());
            call_put_trs_sales_online.setShort("finsert", mst.getFinsert());
            call_put_trs_sales_online.setLong("inq_id", mst.getInqId());
            call_put_trs_sales_online.addBatch();
        }
        call_put_trs_sales_online.executeBatch();
        return serverConn;

    }

    public synchronized Connection putTrsSalesRemittance(Header header, List<TrsSalesRemittance> list, Connection serverConn) throws SQLException {
        LOG.debug("[PUT]");
        CallableStatement call_put_trs_sales_remittance = serverConn.prepareCall(ServerProcedures.put_trs_sales_remittance);
        for (TrsSalesRemittance mst : list) {
            call_put_trs_sales_remittance.setInt("sales_remittance_id", mst.getSalesRemittanceId());
            call_put_trs_sales_remittance.setString("trans_code", mst.getTransCode());
            call_put_trs_sales_remittance.setShort("trans_type", mst.getTransType());

            if (mst.getTransDate() != null) {
                call_put_trs_sales_remittance.setTimestamp("trans_date", new java.sql.Timestamp(mst.getTransDate().getTimeInMillis()));
            } else {
                call_put_trs_sales_remittance.setTimestamp("trans_date", null);
            }

            call_put_trs_sales_remittance.setString("sender_id", mst.getSenderId());
            call_put_trs_sales_remittance.setString("sender_name", mst.getSenderName());
            call_put_trs_sales_remittance.setString("sender_address", mst.getSenderAddress());
            call_put_trs_sales_remittance.setString("sender_city", mst.getSenderCity());
            call_put_trs_sales_remittance.setString("sender_phone", mst.getSenderPhone());
            call_put_trs_sales_remittance.setString("receiver_id", mst.getReceiverId());
            call_put_trs_sales_remittance.setString("receiver_name", mst.getReceiverName());
            call_put_trs_sales_remittance.setString("receiver_address", mst.getReceiverAddress());
            call_put_trs_sales_remittance.setString("receiver_city", mst.getReceiverCity());
            call_put_trs_sales_remittance.setString("receiver_phone", mst.getReceiverPhone());
            call_put_trs_sales_remittance.setFloat("trans_nominal", mst.getTransNominal());
            call_put_trs_sales_remittance.setFloat("trans_cost", mst.getTransCost());
            call_put_trs_sales_remittance.setFloat("trans_total", mst.getTransTotal());
            call_put_trs_sales_remittance.setFloat("trans_fee", mst.getTransFee());
            call_put_trs_sales_remittance.setFloat("trans_save", mst.getTransSave());
            call_put_trs_sales_remittance.setString("trans_reff", mst.getTransReff());
            call_put_trs_sales_remittance.setShort("is_refund", mst.getIsRefund());
            call_put_trs_sales_remittance.setInt("branch_id", mst.getBranchId());
            call_put_trs_sales_remittance.setInt("rekap_id", mst.getRekapId());
            call_put_trs_sales_remittance.setShort("finsert", mst.getFinsert());

            call_put_trs_sales_remittance.addBatch();
        }
        call_put_trs_sales_remittance.executeBatch();
        return serverConn;

    }

    public synchronized Connection putTrsSendHistoryGerai(Header header, List<TrsSendhistory> list, Connection serverConn) throws SQLException {
//        LOG.debug("[PUT]");
        CallableStatement call_put_trs_sendhistory_gerai = serverConn.prepareCall(ServerProcedures.put_trs_sendhistory_gerai);
        for (TrsSendhistory trs : list) {
            call_put_trs_sendhistory_gerai.setShort("branch_id", trs.getBranchId());
            call_put_trs_sendhistory_gerai.setInt("trans_id", trs.getTransId());

            if (trs.getTransDate() != null) {
                call_put_trs_sendhistory_gerai.setDate("trans_date", new java.sql.Date(trs.getTransDate().getTimeInMillis()));
            } else {
                call_put_trs_sendhistory_gerai.setDate("trans_date", null);
            }
            call_put_trs_sendhistory_gerai.setString("trans_type", trs.getTransType());
            call_put_trs_sendhistory_gerai.setShort("trans_status", trs.getTransStatus());
            call_put_trs_sendhistory_gerai.addBatch();
        }

        call_put_trs_sendhistory_gerai.executeBatch();
        return serverConn;
    }

    public synchronized Connection putTrAbsensi(Header header, List<TrAbsensi> list, Connection serverConn) throws SQLException {
        LOG.debug("[PUT]");
        CallableStatement call_put_tr_absensi = serverConn.prepareCall(ServerProcedures.put_tr_absensi);
        for (TrAbsensi trs : list) {
            call_put_tr_absensi.setShort("branch_id", trs.getBranchId());
            call_put_tr_absensi.setInt("id", trs.getId());
            call_put_tr_absensi.setString("nik", trs.getNik());
            call_put_tr_absensi.setString("idabsensi", trs.getIdabsensi());

            if (trs.getJamMasuk() != null) {
                call_put_tr_absensi.setTimestamp("jammasuk", new java.sql.Timestamp(trs.getJamMasuk().getTimeInMillis()));
            } else {
                call_put_tr_absensi.setTimestamp("jammasuk", null);
            }

            if (trs.getJamKeluar() != null) {
                call_put_tr_absensi.setTimestamp("jamkeluar", new java.sql.Timestamp(trs.getJamKeluar().getTimeInMillis()));
            } else {
                call_put_tr_absensi.setTimestamp("jamkeluar", null);
            }

            call_put_tr_absensi.execute();
        }
        return serverConn;
    }

    public synchronized Connection putTrRequest(Header header, List<TrRequest> list, Connection serverConn) throws SQLException {
        LOG.debug("[PUT]");
        CallableStatement call_put_tr_request = serverConn.prepareCall(ServerProcedures.put_tr_request);
        for (TrRequest trs : list) {
            call_put_tr_request.setShort("branch_id", trs.getBranchId());
            call_put_tr_request.setInt("id", trs.getId());
            call_put_tr_request.setString("noaju", trs.getNoaju());
            call_put_tr_request.setShort("gerai", trs.getGeraiSrc());
            call_put_tr_request.setString("nik", trs.getNik());

            if (trs.getMulai() != null) {
                call_put_tr_request.setTimestamp("mulai", new java.sql.Timestamp(trs.getMulai().getTimeInMillis()));
            } else {
                call_put_tr_request.setTimestamp("mulai", null);
            }

            if (trs.getAkhir() != null) {
                call_put_tr_request.setTimestamp("akhir", new java.sql.Timestamp(trs.getAkhir().getTimeInMillis()));
            } else {
                call_put_tr_request.setTimestamp("akhir", null);
            }

            call_put_tr_request.setString("alasan", trs.getAlasan());
            call_put_tr_request.setString("post", trs.getPost());
            call_put_tr_request.setShort("geraitujuan", trs.getGeraiDest());
            call_put_tr_request.setString("posisi", trs.getPosisi());
            call_put_tr_request.setString("spv", trs.getSpv());

            call_put_tr_request.execute();
        }
        return serverConn;
    }

    public synchronized Connection putTrsMitraPaymentList(Header header, List<TrsMitraPaymentMst> list, Connection serverConn) throws SQLException {
        LOG.debug("[PUT]");
        CallableStatement call_put_trs_mitra_payment_mst = serverConn.prepareCall(ServerProcedures.put_trs_mitra_payment_mst);
        for (TrsMitraPaymentMst mst : list) {
            call_put_trs_mitra_payment_mst.setInt("payment_id", mst.getPaymentId());
            call_put_trs_mitra_payment_mst.setInt("mitra_id", mst.getMitraId());
            call_put_trs_mitra_payment_mst.setString("trans_num", mst.getTransNum());

            if (mst.getTransDate() != null) {
                call_put_trs_mitra_payment_mst.setTimestamp("trans_date", new java.sql.Timestamp(mst.getTransDate().getTimeInMillis()));
            } else {
                call_put_trs_mitra_payment_mst.setTimestamp("trans_date", null);
            }

            call_put_trs_mitra_payment_mst.setFloat("subtotal", mst.getSubtotal());
            call_put_trs_mitra_payment_mst.setFloat("discount", mst.getDiscount());
            call_put_trs_mitra_payment_mst.setFloat("tax", mst.getTax());
            call_put_trs_mitra_payment_mst.setFloat("total", mst.getTotal());
            call_put_trs_mitra_payment_mst.setString("notes", mst.getNotes());
            call_put_trs_mitra_payment_mst.setInt("rekap_id", mst.getRekapId());
            call_put_trs_mitra_payment_mst.setInt("status_id", mst.getStatusId());
            call_put_trs_mitra_payment_mst.setString("printed", mst.getPrinted());
            call_put_trs_mitra_payment_mst.setString("insert_log", mst.getInsertLog());
            call_put_trs_mitra_payment_mst.setString("edit_log", mst.getEditLog());
            call_put_trs_mitra_payment_mst.setInt("branch_id", mst.getBranchId());
            call_put_trs_mitra_payment_mst.setInt("relation_id", mst.getRelationId());
            
            LOG.debug("[PUT RELATION_ID] : " + mst.getRelationId());

            call_put_trs_mitra_payment_mst.execute();



            /* DETAILS */
            List<TrsMitraPaymentDetail> details = mst.getDetails();

            CallableStatement call_put_trs_mitra_payment_detail = serverConn.prepareCall(ServerProcedures.put_trs_mitra_payment_detail);
            for (TrsMitraPaymentDetail detail : details) {
                call_put_trs_mitra_payment_detail.setInt("payment_detail_id", detail.getPaymentDetailId());
                call_put_trs_mitra_payment_detail.setInt("payment_id", detail.getPaymentId());
                call_put_trs_mitra_payment_detail.setInt("item_id", detail.getItemId());
                call_put_trs_mitra_payment_detail.setFloat("quantity", detail.getQuantity());
                call_put_trs_mitra_payment_detail.setFloat("quantity2", detail.getQuantity2());
                call_put_trs_mitra_payment_detail.setFloat("price", detail.getPrice());
                call_put_trs_mitra_payment_detail.setFloat("disc", detail.getDisc());
                call_put_trs_mitra_payment_detail.setFloat("tax", detail.getTax());
                call_put_trs_mitra_payment_detail.setFloat("amount", detail.getAmount());
                call_put_trs_mitra_payment_detail.setInt("branch_id", detail.getBranchId());

                call_put_trs_mitra_payment_detail.execute();
            }
        }
        return serverConn;
    }

    public synchronized Connection putMstItemRequest(Header header, List<MstItemRequest> list, Connection serverConn)  throws SQLException {
        LOG.debug("[PUT]");
        CallableStatement call_put_mst_item_request = serverConn.prepareCall(ServerProcedures.put_mst_item_request);
        for (MstItemRequest mst : list) {
            call_put_mst_item_request.setInt("item_request_id", mst.getItemRequestId());
            call_put_mst_item_request.setString("merk", mst.getMerk());
            call_put_mst_item_request.setString("deskripsi", mst.getDeskripsi());
            call_put_mst_item_request.setString("flavour", mst.getFlavour());
            call_put_mst_item_request.setString("pack", mst.getPack());
            call_put_mst_item_request.setString("size", mst.getSize());
            call_put_mst_item_request.setString("insert_log", mst.getInsertLog());
            call_put_mst_item_request.setString("edit_log", mst.getEditLog());
            
            if (mst.getTransDate() != null) {
                call_put_mst_item_request.setTimestamp("trans_date", new java.sql.Timestamp(mst.getTransDate().getTimeInMillis()));
            } else {
                call_put_mst_item_request.setTimestamp("trans_date", null);
            }
            
            call_put_mst_item_request.setInt("branch_id", mst.getBranchId());

            call_put_mst_item_request.addBatch();
        }
        call_put_mst_item_request.executeBatch();
        return serverConn;
    }
    
}
